package online.hxstart.code.concurrent.chapter08;

/**
 * @author huxing
 * @date 2019/7/11 星期四 12:37
 * @describe 处理当queue 达到上限
 */
@FunctionalInterface
public interface DenyPolicy {

  void reject(Runnable runnable, ThreadPool threadPool);

  /**
   * 拒绝并丢任务
   */
  class DiscardDenyPolicy implements DenyPolicy {

    @Override
    public void reject(Runnable runnable, ThreadPool threadPool) {

    }
  }

  /**
   * 拒接并抛出异常
   */
  class AbortDenyPolicy implements DenyPolicy {

    @Override
    public void reject(Runnable runnable, ThreadPool threadPool) {
      throw new RunnableDenyPolicyException("The runnable " + runnable + " will be abort.");
    }
  }

  /**
   * 拒绝 并在提交者所在线程执行
   */
  class RunnerDenyPolicy implements DenyPolicy {

    @Override
    public void reject(Runnable runnable, ThreadPool threadPool) {

      if (!threadPool.isShutdown()) {
        runnable.run();
      }
    }
  }

}
